package com.example.zhinongbao.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.CartAdapter;
import com.example.zhinongbao.utils.CartManager;
import java.util.Locale;

public class CartActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        // 初始化视图
        RecyclerView recyclerView = findViewById(R.id.rv_cart);
        TextView tvTotalPrice = findViewById(R.id.tv_total_price);
        Button btnCheckout = findViewById(R.id.btn_checkout);

        // 设置购物车列表
        CartAdapter adapter = new CartAdapter(CartManager.getInstance().getCartItems());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        // 显示总金额
        double total = CartManager.getInstance().getTotalPrice();
        tvTotalPrice.setText(String.format(Locale.getDefault(), "总计: ¥%.2f", total));

        // 结算按钮点击事件
        btnCheckout.setOnClickListener(v -> {
            // 这里可以跳转到结算页面
            // startActivity(new Intent(this, CheckoutActivity.class));
        });
    }
}